var pageii = null;
var grid = null;
var lei ;
var grid_parentId=null;
$(function() {
	grid = lyGrid({
		id : 'paging',
		l_column : [
				{
					colkey : "id",
					name : "id",
					hide : true
				},
				{
					colkey : "parentId",
					name : "parentId",
					hide : true
				},
				{
					colkey : "name",
					name : "产品名称",
					align : 'left',
					renderData : function(rowindex, data, rowdata, column) {
						var parentId=rowdata.parentId;
						if(parentId==0){
							return rowdata.name;
						}else{
							return rowdata.specification+":"+rowdata.spec_value;
						}
					}
				},
				{
					colkey : "description",
					name : "产品描述",
					width : "70px",
				}/*
					 * , { colkey : "resKey", name : "唯一KEY" }, { colkey :
					 * "resUrl", name : "URL地址" }
					 */,
				{
					colkey : "ishide",
					name : "是否隐藏",
					renderData : function(rowindex, data, rowdata, column) {
						// changan 改 16.5.9
						if (data == "0"||data == null) {
							return "否";
							return '<span class="ishide"><em>o n</em><em class="active">o f f</em></span>'
						} else if (data == "1") {
							return "是";
							return '<span class="ishide"><em class="active">o n</em><em>o f f</em></span>'
						}
					}
				} /*
					 * , { colkey : "description", width : "100px", name : "描述" }
					 */],
		jsonUrl : rootPath + '/producttree/findByPage.shtml',
		checkbox : true,
		usePage : false,
		records : "treelists",
		treeGrid : {
			type : 1,
			tree : true,
			name : 'name',
			id : "id",
			pid : "parentId"
		}
	});
	$("#search").on("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
	$(".addFun").on("click", function() {
		lei = "add";
		addFun();
	});
	// changan ... 点击 。。。
	$("#paging").on("click","#mytable tr td .ishide em",function() {
		var index = $("#mytable tr td .ishide em").index(this);
		$("#mytable tr td .ishide em").eq(index).addClass('active').siblings().removeClass('active');
	});
	// end
	$("#editFun").on("click", function() {
		lei = "edit";
		cover._Body();
		editFun();
	});
	$("#delFun").on("click", function() {
		cover._Body();
		delFun();
	});
	$("#lyGridUp").on("click", function() {// 上移
		var jsonUrl = rootPath + '/producttree/sortUpdate.shtml';
		grid.lyGridUp(jsonUrl);
	});
	$("#lyGridDown").on("click", function() {// 下移
		var jsonUrl = rootPath + '/producttree/sortUpdate.shtml';
		grid.lyGridDown(jsonUrl);
	});
});
function editFun() {
	var cbox = grid.getSelectedCheckbox();
	
	var parentid = $("[cid="+cbox[0]+"]").attr("pid");
	
	var parent=$("[cid="+cbox[0]+"]").parent();
	var parents=$("[cid="+cbox[0]+"]").parent().parent().parent().parent();
	var parents_tree=$("[cid="+cbox[0]+"]").parent().parent().parent().parent().attr("d-tree");
	
	var regex="^[0-9]+-[0-9]+-[0-9]$";
	
	
	var flag=parents_tree.match(regex);
	
	cover.closeAll();
	if(flag!=null){
		//说明是子级
		layer.alert("请选择父级菜单");
		return;
	}
	
	if (cbox.length > 1) {
		layer.alert("只能选中一个");
		return;
	}
	if (cbox == "") {
		layer.alert("必须选中一个");
		return;
	}
	var url = rootPath + '/producttree/editFormUI.shtml?id=' + cbox;
	$.get(url, function(data) {
		pageii = layer.open({
			title : "编辑",
			btn : [ "确定", "取消" ],
			yes : yesLayer,
			type : 1,
			area : [ "800px", "100%" ],
			content : data
		});
	})
	
}
function addFun() {
	var url = rootPath + '/producttree/showUI.shtml';
	$.get(url, function(data) {
		pageii = layer.open({
			title : [ "新增", "background: #fff;" ],
			type : 1,
			btn : [ "确定", "取消" ],
			yes : yesLayer,
			area : [ "800px", "100%" ],
			content : data
		});
	})
	layer.closeAll();
	cover.closeAll();
}
function delFun() {
	cover.closeAll();
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.alert("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/producttree/deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
function yesLayer(index, layers) {
	var jsonData = {};
	var flag = true;
	url = rootPath + '';
	$("#form input.form-control[name]").each(function() {
		jsonData[$(this).attr("name")] = $(this).val();
	})
	jsonData["standard"] = [];
	jsonData["picture"] = [];
	$("#form .area_center[name=standard] tbody tr").each(function(i) {
		
		jsonData["standard"][i] = {};
		jsonData["standard"][i]["id"] = $(this).attr("ids");
		$(this).find("input").each(function() {
			var name = $(this).attr("name");
			jsonData["standard"][i][name] = $(this).val();
			// lpl xiugai
			if (name == "specifications") {
				if(jsonData["standard"][i][name].length==0){
					flag = false;
					alert("请输入规格");
				}
			}
			if (name == "spec_value") {
				if(jsonData["standard"][i][name].length==0){
					flag = false;
					alert("请输入规格值");
				}
			}
			if (name == "prices") {
				if(jsonData["standard"][i][name].length==0){
					alert("请输入价格");
				}else{
					if (isNaN(jsonData["standard"][i][name])) {
						flag = false;
						alert("价格请输入数字");
					}
				}
			}
			if (name == "stocks") {
				if(jsonData["standard"][i][name].length==0){
					alert("请输入库存");
				}else{
					if (isNaN(jsonData["standard"][i][name])) {
						flag = false;
						alert("库存请输入数字");
					}
				}
			}
		})
	})

	$("#uploader img").each(function(i) {
		jsonData["picture"][i] = {};
		jsonData["picture"][i]["src"] = $(this).attr("src");
		jsonData["picture"][i]["id"] = $(this).parent().attr("id");
		jsonData["picture"][i]["name"] = $(this).siblings(".info").text();
	})
	var mainBody = $('#editor').summernote('code');
	jsonData["remark"] = mainBody;
	jsonData["token"] = $("#form input[name=token]").val();
	jsonData["id"] = $("#form input[name=formMap_id]").val();
	var url;
	if(lei == "add"){
		url = rootPath + '/producttree/addForm.shtml';
	} else{
		url = rootPath + '/producttree/editForm.shtml';
	}
	if (flag) {
		$.post(url, jsonData, function(data) {
			alert(data);
			if (data = "success") {
				//
				layer.closeAll();
			}
		})
	} else {
	}
}